package cn.boy.gui.adminstrator;

import cn.boy.gui.LoginActivity;
import cn.boy.home.HomeApplication;
import cn.boy.home.R;
import cn.boy.service.HomeService;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AdminChangePasswordActivity extends Activity implements OnClickListener{

	protected static final String TAG = "AdminChangePasswordActivity";
	EditText txtOld;
	EditText txtNew;
	EditText txtAgain;
	Button yesButton;
	Button cancleButton;
	
//	HomeApplication app;
	
	ProgressDialog pd;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.admin_change_password);
		
//		app = (HomeApplication) getApplicationContext();
		findViews();
		
	}

	private void findViews() {
		txtOld = (EditText)findViewById(R.id.et_passsword_old);
		txtNew = (EditText)findViewById(R.id.et_passsword_new);
		txtAgain = (EditText)findViewById(R.id.et_password_again);
		yesButton = (Button)findViewById(R.id.btn_yes);
		yesButton.setOnClickListener(this);
		cancleButton = (Button)findViewById(R.id.btn_cancle);
		cancleButton.setOnClickListener(this);
	}

	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btn_yes:{
			final String oldPassword = txtOld.getText().toString().trim();
			final String newPassword = txtNew.getText().toString().trim();
			String againPassword = txtAgain.getText().toString().trim();
//			final String no;
//			final boolean flag = app.isFlag();
//			if(flag){
//				no = app.getStudent().getNO();
//			}else{
//				no = app.getAdmin().getNO();
//			}
			
			if(TextUtils.isEmpty(oldPassword)|| TextUtils.isEmpty(newPassword)|| TextUtils.isEmpty(againPassword)){
				Toast.makeText(AdminChangePasswordActivity.this, R.string.date_uncompleted, 1).show();
				break;
			}
			
			
			if(newPassword.equals(againPassword)){
				showDialog();
				
				new Thread(new Runnable() {
					
					public void run() {
						try {
							String retValue = "";
							HomeApplication app = (HomeApplication) getApplicationContext();
							if(app.getStudent() != null){
								retValue = HomeService.updateStudentPassword(app.getStudent().getNO(), oldPassword, newPassword);
							}else{
								retValue = HomeService.updateAdminPassword(app.getAdmin().getNO(), oldPassword, newPassword);
							}
							if(retValue.equals("success")){
								sendHandlerMessage(1);
							}else{
								sendHandlerMessage(2);
								
							}
						} catch (Exception e) {
							Log.e(TAG, e.toString());
							sendHandlerMessage(0);
						}
					}
				}).start();
			}else{
				Toast.makeText(AdminChangePasswordActivity.this, R.string.password_not_same, 1).show();
			}
			
			break;
			
		}
		case R.id.btn_cancle:
			finish();
			break;
		}
	}
	
	private void sendHandlerMessage(int what) {
		Message msg = handler.obtainMessage(what);
		handler.sendMessage(msg);
		
	}
	
	private void showDialog() {
		pd = new ProgressDialog(AdminChangePasswordActivity.this); 
		pd.setMessage(getResources().getString(R.string.password_change_ing));
		pd.show();
	}

	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(pd!=null){
				pd.dismiss();
			}
			switch(msg.what){
			case 0:
				Toast.makeText(AdminChangePasswordActivity.this, R.string.net_unreacgabke, 1).show();
				break;
			case 1:
				Toast.makeText(AdminChangePasswordActivity.this, R.string.password_change_success, 1).show();
				Intent intent = new Intent(AdminChangePasswordActivity.this, LoginActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
				finish();
				HomeApplication app = (HomeApplication) getApplicationContext();
				app.setStudent(null);
				app.setAdmin(null);
				break;
			case 2:
				Toast.makeText(AdminChangePasswordActivity.this, R.string.password_change_error, 1).show();
				break;
			}
		}

		
	};
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			
			Intent intent = new Intent(this, AdminstratorMainActivity.class);
			startActivity(intent);
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
